package cc.codeshop.net;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.IntentService;
import android.content.Intent;

public class ApkDownloadService extends IntentService {

	public ApkDownloadService() {
		super("ApkDownload");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		
	}
	
	private void download(String apkURL) throws IOException {
		File apkFile = FileUtil.getCacheFile(this, "apk", "abc.apk");
		File tmpFile = FileUtil.getCacheFile(this, "apk", "abc_apk");
		long lastModified = 0;
		long length = 0;
		if(apkFile.exists()) {		//本地存在，判断服务端是否有更改，有则下载，否则不下载
			lastModified = apkFile.lastModified();
		} else {		//本地不存在，新下载或续传
			if(tmpFile.exists()) {			//存在临时文件 -> 续传
				length = tmpFile.length();
			} else {		//不存在临时文件 -> 
			}
		}
		if(lastModified > 0) {
			
		}
		
		URL url = new URL(apkURL);
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
		conn.connect();
		InputStream is = conn.getInputStream();
		FileOutputStream fos = new FileOutputStream(apkFile);
		IOUtils.copyStream(is, fos);
		IOUtils.closeSilently(is);
		IOUtils.closeSilently(fos);
	}

}
